package cn.wolfcode.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import cn.wolfcode.domain.Appointment;
import cn.wolfcode.domain.Consumption;
import cn.wolfcode.enums.AppointmentStatusEnum;
import cn.wolfcode.mapper.ConsumptionMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IAppointmentService;
import cn.wolfcode.service.IConsumptionService;
import cn.wolfcode.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class ConsumptionServiceImpl implements IConsumptionService {

    //日志
    //public static  final Logger log = LoggerFactory.getLogger(ConsumptionServiceImpl.class);

    @Autowired
    private ConsumptionMapper consumptionMapper;
    @Autowired
    private IAppointmentService appointmentService;

    @Override
    public Consumption save(String appointmentAno) {
        //根据预约单的流水号查询预约单的对象
        Appointment appointment = appointmentService.selectByAno(appointmentAno);
        //修改预约单状态为消费中状态
        appointmentService.updateStatus(appointment.getId(), AppointmentStatusEnum.CONSUME.getValue());
        //创建结算单对象
        Consumption consumption = new Consumption();
        consumption.setAppointmentAno(appointmentAno);

        //流水单号设置到结算单里面关联
        consumption.setAppointmentAno(appointmentAno);



        //生成流水号，并设置到结算单中关联
        Date date = new Date();
        String format = DateUtil.format(date,"yyyyMMddHHmmss");
        String numbers = RandomUtil.randomNumbers(5);
        consumption.setCno(format+numbers);
        //预约单中获取客户名称，设置到结算单中
        consumption.setCustomerName(appointment.getContactName());
        //预约单中获取客户电话，设置到结算单中
        consumption.setCustomerTel(appointment.getContactTel());
        //获取门店，设置到结算单中
        consumption.setBusiness(appointment.getBusiness());
        //进店时间设置为当前时间
        consumption.setCheckinTime(date);
        //创建时间
        consumption.setCreateTime(date);
        //创建人
        consumption.setCreateUser(UserContext.getCurrentUser());
        consumptionMapper.insert(consumption);

        //业务日志记录
        log.info("生成流水号为{}的结算单，关联流水号为{}的预约单,该预约单的状态转为{}",
                format+numbers,appointmentAno,AppointmentStatusEnum.CONSUME.getName());

        return consumption;
    }

    @Override
    public void delete(Long id) {
        consumptionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(Consumption consumption) {
        consumptionMapper.updateByPrimaryKey(consumption);
    }

    @Override
    public Consumption get(Long id) {
        return consumptionMapper.selectByPrimaryKey(id);
    }


    @Override
    public PageInfo<Consumption> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize()); //对下一句sql进行自动分页
        List<Consumption> consumptions = consumptionMapper.selectForList(qo); //里面不需要自己写limit
        return new PageInfo<Consumption>(consumptions);
    }
}
